home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-06 / an201x.zip / DATAFILE.C < prev    next >
Text File  |  1991-12-19  |  11KB  |  400 lines

  1. /*****************************************************************************
  2. * Project:  Workstation inventory
  3. * File:        DATAFILE.C
  4. * Author:    Morgan B. Adair
  5. * Date:        12/15/91
  6. *****************************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <nit.h>
  12. #include "datafile.h"
  13. #include "btrieve.h"
  14. #include "recdecl.h"
  15.  
  16. #define MOUSE_DRVR_MAJOR_VER    0xFF00
  17. #define MOUSE_DRVR_MINOR_VER    0x00FF
  18.  
  19. void DisplayType(int displayCode);
  20. void OutputData(WS_INVENTORY_RECORD *entry);
  21.  
  22. char positionBlock[128];                                    /* Btrieve file position block */
  23.  
  24. void LoadBtrieve(void)
  25. {
  26.     system("btrieve");
  27. }
  28.  
  29. int UnloadBtrieve(void)
  30. {
  31.     return(BTRV(BT_STOP, NULL, NULL, NULL, NULL, 0));
  32. }
  33.  
  34. int BtrieveIsLoaded(void)
  35. {
  36.     int    status;
  37.     char    dataBuffer[5];
  38.     int    dataLength = 5;
  39.  
  40.     status = BTRV(BT_VERSION, NULL, dataBuffer, &dataLength, NULL, 0);
  41.     if (status == BT_SUCCESS)
  42.         return(TRUE);
  43.     else
  44.         return(FALSE);
  45. }
  46.  
  47. int DataFileExists(char *filePath)
  48. {
  49.     FILE    *dataFile;
  50.  
  51.     /* assume that if file is there, it's the right file */
  52.     dataFile = fopen(filePath, "r");
  53.     if (dataFile) {
  54.         fclose(dataFile);
  55.         return(TRUE);
  56.     } else
  57.         return(FALSE);
  58. }
  59.  
  60. int OpenDataFile(char *fileName)
  61. {
  62.     unsigned        bufferLength;
  63.     WS_INVENTORY_RECORD    entry;
  64.     int             status;
  65.  
  66.     /* Figure out how long the record is */
  67.     bufferLength = sizeof(WS_INVENTORY_RECORD);
  68.  
  69.     /* Call BTRIEVE Open operation */
  70.     status = BTRV(BT_OPEN, positionBlock, (char *)&entry, (int *)&bufferLength, fileName, 1);
  71.  
  72.     /* Return the result */
  73.     return(status);
  74. }
  75.  
  76. int CreateDataFile(char *filePath)
  77. {
  78.     unsigned     bufferLength;
  79.     FILE_SPEC    fileBuffer;
  80.     int         status;
  81.  
  82.     /* Create a new Btrieve file for the program file data */
  83.     /* Initialize buffer */
  84.     memset(&fileBuffer, 0, sizeof(fileBuffer));
  85.  
  86.     /* Define the global file paramaters */
  87.     fileBuffer.recordLength = sizeof(WS_INVENTORY_RECORD);
  88.     fileBuffer.pageSize     = 1024;                /* use Btrieve's default page size */
  89.     fileBuffer.fileFlags    = 0;
  90.     fileBuffer.indexCount   = NUMBER_OF_KEYS;
  91.     fileBuffer.preAlloc     = 0;
  92.  
  93.     /* Key 0: network/node (1 key, 2 segments) */
  94.     /* Segment 1:  network address, binary key */
  95.     fileBuffer.keyBuf[0].keyPos  = 1;
  96.     fileBuffer.keyBuf[0].keyLen  = 4;
  97.     /* BT_KEY_SEG means another segment of this key follows */
  98.     fileBuffer.keyBuf[0].keyFlag = BT_KEY_EXT_TYPE | BT_KEY_SEG;
  99.     fileBuffer.keyBuf[0].keyType = BT_KEY_BIN;
  100.  
  101.     /* Segment 2:  node address, binary key */
  102.     fileBuffer.keyBuf[1].keyPos  = 5;
  103.     fileBuffer.keyBuf[1].keyLen  = 6;
  104.     /* last segment in this key, so no BT_KEY_SEG on the flags */
  105.     fileBuffer.keyBuf[1].keyFlag = BT_KEY_EXT_TYPE;
  106.     fileBuffer.keyBuf[1].keyType = BT_KEY_BIN;
  107.  
  108.     /* Figure out the data buffer length */
  109.     bufferLength = sizeof(fileBuffer);
  110.  
  111.     /*--- Call Btrieve with a Create request. ---*/
  112.     status = BTRV(BT_CREATE, positionBlock, (char *)&fileBuffer, (int *)&bufferLength, filePath, 0);
  113.  
  114.     return(status);
  115. }
  116.  
  117. int ListItems(void)
  118. {
  119.     int            status;
  120.     unsigned        dataLength;
  121.     char            keyBuffer[80];
  122.     WS_INVENTORY_RECORD    entry;
  123.  
  124.     /* Figure out the data buffer length */
  125.     dataLength = sizeof(WS_INVENTORY_RECORD);
  126.  
  127.     /* Get the first item in the database */
  128.     status = BTRV(BT_GET_FIRST, positionBlock, (char *)&entry, (int *)&dataLength, keyBuffer, 0);
  129.  
  130.     /* As long as there are more, keep printing them */
  131.     while (status != BT_END_OF_FILE) {
  132.  
  133.         /* Die on errors other than ERR_END_OF_FILE */
  134.         if (status != BT_SUCCESS)
  135.             return(status);
  136.  
  137.         /* Show item to user */
  138.         OutputData(&entry);
  139.  
  140.         /* Step forwards to the next item */
  141.         status = BTRV(BT_GET_NEXT, positionBlock, (char *)&entry, (int *)&dataLength, keyBuffer, 0);
  142.     }
  143.  
  144.     return(BT_SUCCESS);
  145. }
  146.  
  147. void OutputData(WS_INVENTORY_RECORD *entry)
  148. {
  149.     int    i;
  150.  
  151.     printf("Network/node: ");
  152.     for (i=0; i<4; i++)
  153.         printf("%X", entry->network[i]);
  154.     printf("/");
  155.     for (i=0; i<6; i++)
  156.         printf("%X", entry->node[i]);
  157.     printf("\n");
  158.  
  159.     printf("\tUser name (on default server): %s\n", entry->userName);
  160.  
  161.     printf("\tIPX Version: %d.%02d\n", entry->IPXmajorVersion, entry->IPXminorVersion);
  162.     printf("\tSPX Version: %d.%02d\n", entry->SPXmajorVersion, entry->SPXminorVersion);
  163.  
  164.     printf("\tShell Version: %d.%d, Rev %c\n", entry->shellMajorVersion,
  165.                            entry->shellMinorVersion,
  166.                            entry->shellRevisionLevel+'A');
  167.  
  168.     printf("\tMachine model: ");
  169.     switch (entry->machineModel) {
  170.         case    0x2D    :    printf("\tCompaq Portable\n");
  171.                     break;
  172.         case    0x9A    :    printf("\tCompaq Portable Plus\n");
  173.                     break;
  174.         case    0xF8    :    printf("\tIBM PS/2 Model 80\n");
  175.                     break;
  176.         case    0xF9    :    printf("\tIBM PC Convertible\n");
  177.                     break;
  178.         case    0xFA    :    printf("\tIBM PS/2 Model 30\n");
  179.                     break;
  180.         case    0xFB    :    printf("\tIBM PC XT\n");
  181.                     break;
  182.         case    0xFC    :    switch (entry->machineSubmodel) {
  183.                         case    0    :
  184.                         case    1    :    printf("\tIBM PC/AT\n");
  185.                                     break;
  186.                         case    2    :    printf("\tIBM PC/XT286\n");
  187.                                     break;
  188.                         case    4    :    printf("\tIBM PS/2 Model 50\n");
  189.                                     break;
  190.                         case    5    :    printf("\tIBM PS/2 Model 60\n");
  191.                                     break;
  192.                         case    0xB    :    printf("\tIBM PS/1\n");
  193.                                     break;
  194.                         default        :    printf("\tIBM PC/AT or PS class\n");
  195.                                     break;
  196.                     }
  197.                     break;
  198.         case    0xFD    :    printf("\tIBM PCjr\n");
  199.                     break;
  200.         case    0xFE    :    printf("\tIBM PC/XT, Portable PC, or Compaq DeskPro\n");
  201.                     break;
  202.         case    0xFF    :    printf("\tIBM PC\n");
  203.                     break;
  204.         default        :    printf("\tUnknown machine type %d\n", entry->machineModel);
  205.                     break;
  206.     }
  207.  
  208.     switch (entry->processorType) {
  209.         case     0x0086    :    printf("\t8086 or 8088 processor\n");
  210.                     break;
  211.         case     0x0286    :    printf("\t80286 processor\n");
  212.                     break;
  213.         case     0x0386    :    printf("\t80386DX or 80386SX processor\n");
  214.                     break;
  215.         case     0x0486    :    printf("\t80486DX or 80486SX processor\n");
  216.                     break;
  217.         default        :    printf("\tUnable to identify CPU\n");
  218.                     break;
  219.     }
  220.  
  221.     printf("\tCoprocessor: ");
  222.     switch (entry->coprocessorType) {
  223.         case    0    :    printf("No coprocessor\n");
  224.                     break;
  225.         case    0x0087    :    printf("8087 coprocessor\n");
  226.                     break;
  227.         case    0x0287    :    printf("80287 coprocessor\n");
  228.                     break;
  229.         case    0x0387    :    printf("80387 coprocessor\n");
  230.                     break;
  231.         default        :    printf("Error getting coprocessor type: %u\n", entry->coprocessorType);
  232.                     break;
  233.     }
  234.  
  235.     printf("\tBase memory: %dK\n", entry->conventionalMemoryK);
  236.  
  237.     printf("\tExtended memory: %dK\n", entry->extendedMemoryK);
  238.  
  239.     printf("\tExpanded memory: %dK\n", entry->expandedMemoryK);
  240.  
  241.     printf("\tVideo hardware:  ");
  242.     DisplayType(entry->videoHardware);
  243.  
  244.     if (entry->alternateVideoHardware) {
  245.         printf("\tAlternate display hardware:  ");
  246.         DisplayType(entry->alternateVideoHardware);
  247.     }
  248.  
  249.     printf("\tDOS version: %d.%02d\n", entry->DOSversionMajor, entry->DOSversionMinor);
  250.  
  251.     if (entry->NICtype)
  252.         printf("\tNIC type: %s\n", entry->NICtype);
  253.     else
  254.         printf("\tNIC type: unable to identify\n");
  255.  
  256.     printf("\t%d floppy drives\n", entry->numFloppyDrives);
  257.  
  258.     printf("\t%d printers installed\n", entry->numPrintersInstalled);
  259.  
  260.     printf("\t%d serial ports\n", entry->numSerialPorts);
  261.  
  262.     if (entry->mouseDriverVersion)
  263.         printf("\tMouse driver version: %d.%02d\n",
  264.             (entry->mouseDriverVersion & MOUSE_DRVR_MAJOR_VER) >> 8,
  265.              entry->mouseDriverVersion & MOUSE_DRVR_MINOR_VER);
  266.  
  267.     if (!(entry->mouseType))
  268.         printf("\tMouse type: no mouse found\n");
  269.     else {
  270.         printf("\tMouse type: ");
  271.         switch (entry->mouseType) {
  272.             case 1    :    printf("Bus mouse\n");
  273.                     break;
  274.             case 2    :    printf("Serial mouse\n");
  275.                     break;
  276.             case 3    :    printf("InPort mouse\n");
  277.                     break;
  278.             case 4    :    printf("PS/2 mouse\n");
  279.                     break;
  280.             case 5    :    printf("Hewlet-Packard mouse\n");
  281.                     break;
  282.             default    :    printf("Unknown mouse type\n");
  283.                     break;
  284.         }
  285.     }
  286.  
  287.     for (i=0; i<entry->numFloppyDrives; i++) {
  288.         printf("\tFloppy drive %d: ", i);
  289.         switch (entry->floppyDriveType[i]) {
  290.             case    1    :    printf("360KB\n");
  291.                         break;
  292.             case    2    :    printf("1.2MB\n");
  293.                         break;
  294.             case    3    :    printf("720KB\n");
  295.                         break;
  296.             case    4    :    printf("1.44MB\n");
  297.                         break;
  298.             default        :    printf("unable to identify type\n");
  299.                         break;
  300.         }
  301.     }
  302.  
  303.     for (i=0; i<entry->numHardDrives; i++)
  304.         printf("\tHard drive %d: %ld bytes\n",
  305.             i, entry->hardDriveSize[i]);
  306. }
  307.  
  308.  
  309. void DisplayType(int displayCode)
  310. {
  311.     switch    (displayCode) {
  312.         case 0    :    printf("No display\n");
  313.                 break;
  314.         case 1    :    printf("Monochrome with 5151 monitor\n");
  315.                 break;
  316.         case 2    :    printf("CGA with 5153/4 monitor\n");
  317.                 break;
  318.         case 4    :    printf("EGA with 5153/4 montior\n");
  319.                 break;
  320.         case 5    :    printf("EGA with 5151 monitor\n");
  321.                 break;
  322.         case 6    :    printf("PGS with 5175 monitor\n");
  323.                 break;
  324.         case 7    :    printf("VGA with analog monochrome monitor\n");
  325.                 break;
  326.         case 8    :    printf("VGA with analog color monitor\n");
  327.                 break;
  328.         case 11    :    printf("Model 30 with analog monochrome monitor (MCGA)\n");
  329.                 break;
  330.         case 12    :    printf("Model 30 with analog color monitor (MCGA)\n");
  331.                 break;
  332.         default    :       printf("Unable to identify graphics hardware\n");
  333.     }
  334. }
  335.  
  336. int SearchItem(WS_INVENTORY_RECORD *entry)
  337. {
  338.     int        status;
  339.     unsigned    dataLength;
  340.     int        i;
  341.     char        keyBuffer[KEY0_SIZE];
  342.     char        *charPtr;
  343.     WS_INVENTORY_RECORD    oldEntry;
  344.  
  345.     /* Copy the key data to somewhere that can be overwritten */
  346.     charPtr = (char *)entry;
  347.     for (i=0; i<KEY0_SIZE; i++)
  348.         keyBuffer[i] = *charPtr++;
  349.  
  350.     /* Figure out the data buffer size */
  351.     dataLength = sizeof(WS_INVENTORY_RECORD);
  352.  
  353.     /* Find the item matching the specification */
  354.     status = BTRV(BT_GET_EQUAL, positionBlock, (char *)&oldEntry, (int *)&dataLength, keyBuffer, 0);
  355.  
  356.     return(status);
  357. }
  358.  
  359. int InsertItem(WS_INVENTORY_RECORD *entry)
  360. {
  361.     int    status;
  362.     int    dataLength;
  363.     char    keyBuffer[KEY0_SIZE];
  364.  
  365.     /* Figure out the data buffer length */
  366.     dataLength = sizeof(WS_INVENTORY_RECORD);
  367.  
  368.     /* Call Btrieve with Insert request */
  369.     status = BTRV(BT_INSERT, positionBlock, (char *)entry, &dataLength, keyBuffer, 0);
  370.     return(status);
  371. }
  372.  
  373. int UpdateItem(WS_INVENTORY_RECORD *entry)
  374. {
  375.     int    status;
  376.     int    dataLength;
  377.     char    keyBuffer[KEY0_SIZE];
  378.  
  379.     /* Figure out the data buffer length */
  380.     dataLength = sizeof(WS_INVENTORY_RECORD);
  381.  
  382.     /* Call Btrieve with Update request */
  383.     status = BTRV(BT_UPDATE, positionBlock, (char *)entry, &dataLength, keyBuffer, 0);
  384.     return(status);
  385. }
  386.  
  387. int CloseDataFile(void)
  388. {
  389.     char            keyBuffer[KEY0_SIZE];
  390.     int            bufferLength;
  391.     WS_INVENTORY_RECORD    entry;
  392.     int            status;
  393.  
  394.     /* Call Btrieve with Close operation */
  395.     status = BTRV(BT_CLOSE, positionBlock, (char *)&entry, &bufferLength, keyBuffer, 0);
  396.  
  397.     return(status);
  398. }
  399.  
  400.